<?php
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;

class AnnexController extends AdminBaseController
{
    public function initialize()
    {
        parent::initialize();
    }

    /*附件管理*/
    public function index()
    {
        $annex_name = $this->request->param("annex_name");
        $results = Db::name("annex")->alias('a')->field("a.*,c.cate_name")
            ->leftJoin('cate c','a.stuff_type=c.id')
            ->where('annex_name', 'like', '%' . $annex_name . '%')
            ->paginate(10);

        $page = $results->render();
        $this->assign("results", $results);
        $this->assign("page", $page);
        return $this->fetch();
    }

    /*附件添加--跳转*/
    public function add()
    {
        $cate_name = Db::name('cate')
        ->where('type', 6)
        ->where('if_show',1)
        ->where('is_delete','0')
        ->select();

        $this->assign('cate_name', $cate_name);
        return $this->fetch();
    }

    /*附件添加--保存*/
    public function add_post()
    {
        $data = $this->request->param();
        if ($data['price'] < 0){
            $this->error('附件价格不可为负数');
        }

        $data['add_time'] = time();
        $data['add_user'] = cmf_get_current_admin_id();
        Db::name("annex")->insert($data);
        $this->success("添加成功！", url("Annex/index"));
    }

    /*附件编辑--跳转传值*/
    public function edit()
    {
        $id = $this->request->param('id');
        $result = Db::name("annex")
            ->where('id', $id)
            ->find();

        $this->assign($result);

        $cate_name = Db::name('cate')
            ->where('type', 6)
            ->where('if_show',1)
            ->where('is_delete','0')
            ->select();

        $this->assign('cate_name', $cate_name);

        return $this->fetch();
    }

    /*附件编辑--保存*/
    public function edit_post()
    {
        $data = $this->request->param();
        if ($data['price'] < 0){
            $this->error('附件价格不可为负数');
        }
        Db::name("annex")->update($data);
        $this->success("保存成功", url("Annex/index"));
    }

    /*附件删除*/
    public function delete()
    {
        $data = $this->request->param();

        Db::name("annex")->delete($data);
        $this->success("删除成功", url("Annex/index"));
    }
}